/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package videoProcessing;

import java.io.IOException;

/**
 *
 * @author damfercer
 */
public class FFMPEGVideoResizer implements VideoResizer{  
    
    private final String uriFfmpeg="/Recruitery/ffmpeg";
    //private final String uriFfmpeg="/Users/damianfernandez/Recruitery/ffmpeg";
    //private final String uriFfmpeg="C:\\Users\\Alvaro\\Desktop\\ffmpeg.exe";
    
    @Override
    public void getMp4Video(String fromUri, String toUri, Integer maxpixels) throws InterruptedException, IOException, RuntimeException{
        String execute= uriFfmpeg+"  -i "+fromUri+" -vf scale='if(gt(iw,ih),"+maxpixels.toString()+",-1)':'if(gt(ih,iw),"+maxpixels.toString()+",-1)' -acodec aac -strict -2  -vcodec libx264  -qscale:v 5 -qscale:a 3 "+toUri;            
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(execute);
        // any error message?
        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERR");            
        // any output?
        StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUT");
        // kick them off
        errorGobbler.start();
        outputGobbler.start();
        // any error???
        int exitVal = proc.waitFor();
        if(exitVal!=0){
            throw new RuntimeException();
        }
    }
    
    @Override
    public void getOggVideo(String fromUri, String toUri, Integer maxpixels) throws InterruptedException, IOException, RuntimeException{
        String execute= uriFfmpeg+"  -i "+fromUri+" -vf scale='if(gt(iw,ih),"+maxpixels.toString()+",-1)':'if(gt(ih,iw),"+maxpixels.toString()+",-1)' -acodec libvorbis  -vcodec libtheora  -qscale:v 5 -qscale:a 3 "+toUri;
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(execute);
        // any error message?
        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERR");            
        // any output?
        StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUT");
        // kick them off
        errorGobbler.start();
        outputGobbler.start();
        // any error???
        int exitVal = proc.waitFor();
        if(exitVal!=0){
            throw new RuntimeException();
        }
    }
    
    @Override
    public void getWebmVideo(String fromUri, String toUri, Integer maxpixels) throws InterruptedException, IOException, RuntimeException{
        String execute= uriFfmpeg+"  -i "+fromUri+" -vf scale='if(gt(iw,ih),"+maxpixels.toString()+",-1)':'if(gt(ih,iw),"+maxpixels.toString()+",-1)' -qscale:v 5 -qscale:a 3 "+toUri;
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(execute);
        // any error message?
        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERR");            
        // any output?
        StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUT");
        // kick them off
        errorGobbler.start();
        outputGobbler.start();
        // any error???
        int exitVal = proc.waitFor();
        if(exitVal!=0){
            throw new RuntimeException();
        }
    }    
}


